<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="课程编码" prop="courseCode">
            <el-input v-model="formData.courseCode" placeholder="请输入课程编码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="课程名称" prop="courseName">
            <el-input v-model="formData.courseName" placeholder="请输入课程名称" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="课程类型" prop="courseType">
            <el-select v-model="formData.courseType" placeholder="请选择课程类型" style="width: 100%">
              <el-option label="必修课" value="必修课" />
              <el-option label="选修课" value="选修课" />
              <el-option label="实践课" value="实践课" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="课程类别" prop="courseCategory">
            <el-select v-model="formData.courseCategory" placeholder="请选择课程类别" style="width: 100%">
              <el-option label="专业课" value="专业课" />
              <el-option label="基础课" value="基础课" />
              <el-option label="公共课" value="公共课" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="课程学时" prop="creditHours">
            <el-input-number v-model="formData.creditHours" :min="0" :max="999" style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="理论学时" prop="theoryHours">
            <el-input-number v-model="formData.theoryHours" :min="0" :max="999" style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="实践学时" prop="practiceHours">
            <el-input-number v-model="formData.practiceHours" :min="0" :max="999" style="width: 100%" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="学分" prop="credits">
            <el-input-number v-model="formData.credits" :min="0" :max="10" :precision="1" :step="0.5" style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="formData.status">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="先修课程" prop="prerequisiteCourses">
        <el-input v-model="formData.prerequisiteCourses" placeholder="请输入先修课程ID，多个用逗号分隔" />
      </el-form-item>

      <el-form-item label="课程描述" prop="description">
        <el-input v-model="formData.description" type="textarea" :rows="3" placeholder="请输入课程描述" />
      </el-form-item>

      <el-form-item label="课程目标" prop="objectives">
        <el-input v-model="formData.objectives" type="textarea" :rows="3" placeholder="请输入课程目标" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { CourseApi, Course } from '@/api/ao/course'

/** 课程信息 表单 */
defineOptions({ name: 'CourseForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  courseId: undefined,
  courseCode: undefined,
  courseName: undefined,
  groupId: undefined,
  courseType: undefined,
  courseCategory: undefined,
  creditHours: undefined,
  theoryHours: undefined,
  practiceHours: undefined,
  credits: undefined,
  prerequisiteCourses: undefined,
  description: undefined,
  objectives: undefined,
  status: 1,
})
const formRules = reactive({
  courseCode: [{ required: true, message: '课程编码不能为空', trigger: 'blur' }],
  courseName: [{ required: true, message: '课程名称不能为空', trigger: 'blur' }],
  courseType: [{ required: true, message: '课程类型不能为空', trigger: 'change' }],
  courseCategory: [{ required: true, message: '课程类别不能为空', trigger: 'change' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'change' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await CourseApi.getCourse(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as Course
    if (formType.value === 'create') {
      await CourseApi.createCourse(data)
      message.success(t('common.createSuccess'))
    } else {
      await CourseApi.updateCourse(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    courseId: undefined,
    courseCode: undefined,
    courseName: undefined,
    groupId: undefined,
    courseType: undefined,
    courseCategory: undefined,
    creditHours: undefined,
    theoryHours: undefined,
    practiceHours: undefined,
    credits: undefined,
    prerequisiteCourses: undefined,
    description: undefined,
    objectives: undefined,
    status: 1,
  }
  formRef.value?.resetFields()
}
</script>